package com.zr.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import com.zr.dao.BaseDao;
import com.zr.dao.UserDao;
import com.zr.dao.impl.BaseDaOImpl;
import com.zr.model.Car;
import com.zr.model.Orders;
import com.zr.model.User;
import com.zr.service.AddCarService;

import net.sf.json.JSONObject;
@Service("addcarService")
public class AddCarServiceImpl<T> extends BaseDaOImpl<T>implements AddCarService{
	@Resource
	private BaseDao<User>  baseDao;
	@Override
	public Car addCar(String plate, String volume, String weight, String purchasing, String state, String driver,String position) {
	
		Car car=new Car();
		car.setPlate(plate);
		car.setDriver(driver);
		car.setPurchasing(purchasing);
		car.setState(state);
		car.setVolume(volume);
		car.setWeight(weight);
		car.setPosition(position);
		this.getCurrentSession().save(car);
		return car;
	}
	@Override
	public Car updateCar(String plate, String volume, String weight, String purchasing, String state, String driver,String position,int cid) {
		Car car=(Car)this.getCurrentSession().load(Car.class, Integer.valueOf(cid));
        car.setPlate(plate);
        car.setDriver(driver);
        car.setPurchasing(purchasing);
        car.setState(state);
        car.setWeight(weight);
        car.setVolume(volume);
        car.setPosition(position);     
		return car;
	}
	@Override
	public Car deleteCar(int cid) {
		Car car=(Car)this.getCurrentSession().get(Car.class, Integer.valueOf(cid));
		this.getCurrentSession().delete(car);
		return car;
	}
	@Override
	public Orders updateOrderstatus(String plate, String Orderstatus, int oid) {		
		Orders orders=(Orders)this.getCurrentSession().load(Orders.class, Integer.valueOf(oid));
		orders.setOrderstatus(Orderstatus);
		orders.setPlate(plate);
		return orders;
	}
	@Override
	public Car updateCarPosition(String position,String state, int cid) {
		Car car=(Car)this.getCurrentSession().get(Car.class, Integer.valueOf(cid));
		car.setPosition(position);
		car.setState(state);
		return car;
	}
	@Override
	public int findPlates(String plate) {
		String hql = "from Car u where u.plate=:plate";	
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("plate", plate);
		List<Car> ts = query.list();
		int i=(ts.size());
		return i; 
		
	}
	

}
